{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "一、面部检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "# json\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"17623c51f2a3403d9048528e6032cd76\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-1.cognitiveservices.azure.cn//face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1035455085,2192695287&fm=26&gp=0.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'd81a4ecb-ca77-4ba2-b10a-665f18f5c5cb',\n",
       "  'faceRectangle': {'top': 159, 'left': 193, 'width': 163, 'height': 163},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 15.7, 'roll': -2.4, 'yaw': 18.2},\n",
       "   'gender': 'male',\n",
       "   'age': 25.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.005,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.061,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.93,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.001},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.14},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.71},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.09},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.02,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.83},\n",
       "     {'color': 'gray', 'confidence': 0.43},\n",
       "     {'color': 'other', 'confidence': 0.35},\n",
       "     {'color': 'blond', 'confidence': 0.07},\n",
       "     {'color': 'red', 'confidence': 0.03}]}}}]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "二、json转译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'd81a4ecb-ca77-4ba2-b10a-665f18f5c5cb',\n",
       "  'faceRectangle': {'top': 159, 'left': 193, 'width': 163, 'height': 163},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 15.7, 'roll': -2.4, 'yaw': 18.2},\n",
       "   'gender': 'male',\n",
       "   'age': 25.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.005,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.061,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.93,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.001},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.14},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.71},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.09},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.02,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.83},\n",
       "     {'color': 'gray', 'confidence': 0.43},\n",
       "     {'color': 'other', 'confidence': 0.35},\n",
       "     {'color': 'blond', 'confidence': 0.07},\n",
       "     {'color': 'red', 'confidence': 0.03}]}}}]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2\n",
    "results = response.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "三、pandas表格数据化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.smile</th>\n",
       "      <th>faceAttributes.headPose.pitch</th>\n",
       "      <th>faceAttributes.headPose.roll</th>\n",
       "      <th>faceAttributes.headPose.yaw</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>...</th>\n",
       "      <th>faceAttributes.noise.value</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "      <th>faceAttributes.accessories</th>\n",
       "      <th>faceAttributes.occlusion.foreheadOccluded</th>\n",
       "      <th>faceAttributes.occlusion.eyeOccluded</th>\n",
       "      <th>faceAttributes.occlusion.mouthOccluded</th>\n",
       "      <th>faceAttributes.hair.bald</th>\n",
       "      <th>faceAttributes.hair.invisible</th>\n",
       "      <th>faceAttributes.hair.hairColor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d81a4ecb-ca77-4ba2-b10a-665f18f5c5cb</td>\n",
       "      <td>159</td>\n",
       "      <td>193</td>\n",
       "      <td>163</td>\n",
       "      <td>163</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.7</td>\n",
       "      <td>-2.4</td>\n",
       "      <td>18.2</td>\n",
       "      <td>male</td>\n",
       "      <td>...</td>\n",
       "      <td>0.09</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>[]</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0.02</td>\n",
       "      <td>False</td>\n",
       "      <td>[{'color': 'black', 'confidence': 1.0}, {'colo...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  d81a4ecb-ca77-4ba2-b10a-665f18f5c5cb                159   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 193                  163                   163   \n",
       "\n",
       "   faceAttributes.smile  faceAttributes.headPose.pitch  \\\n",
       "0                   0.0                           15.7   \n",
       "\n",
       "   faceAttributes.headPose.roll  faceAttributes.headPose.yaw  \\\n",
       "0                          -2.4                         18.2   \n",
       "\n",
       "  faceAttributes.gender  ...  faceAttributes.noise.value  \\\n",
       "0                  male  ...                        0.09   \n",
       "\n",
       "   faceAttributes.makeup.eyeMakeup  faceAttributes.makeup.lipMakeup  \\\n",
       "0                            False                            False   \n",
       "\n",
       "   faceAttributes.accessories faceAttributes.occlusion.foreheadOccluded  \\\n",
       "0                          []                                     False   \n",
       "\n",
       "   faceAttributes.occlusion.eyeOccluded  \\\n",
       "0                                 False   \n",
       "\n",
       "   faceAttributes.occlusion.mouthOccluded  faceAttributes.hair.bald  \\\n",
       "0                                   False                      0.02   \n",
       "\n",
       "   faceAttributes.hair.invisible  \\\n",
       "0                          False   \n",
       "\n",
       "                       faceAttributes.hair.hairColor  \n",
       "0  [{'color': 'black', 'confidence': 1.0}, {'colo...  \n",
       "\n",
       "[1 rows x 38 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-3\n",
    "import pandas as pd # 定义模块名字\n",
    "df_face = pd.json_normalize(results)\n",
    "df_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "四、数据取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d81a4ecb-ca77-4ba2-b10a-665f18f5c5cb']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceID = df_face['faceId'].values.tolist()\n",
    "faceID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['faceId', 'faceRectangle.top', 'faceRectangle.left',\n",
       "       'faceRectangle.width', 'faceRectangle.height', 'faceAttributes.smile',\n",
       "       'faceAttributes.headPose.pitch', 'faceAttributes.headPose.roll',\n",
       "       'faceAttributes.headPose.yaw', 'faceAttributes.gender',\n",
       "       'faceAttributes.age', 'faceAttributes.facialHair.moustache',\n",
       "       'faceAttributes.facialHair.beard',\n",
       "       'faceAttributes.facialHair.sideburns', 'faceAttributes.glasses',\n",
       "       'faceAttributes.emotion.anger', 'faceAttributes.emotion.contempt',\n",
       "       'faceAttributes.emotion.disgust', 'faceAttributes.emotion.fear',\n",
       "       'faceAttributes.emotion.happiness', 'faceAttributes.emotion.neutral',\n",
       "       'faceAttributes.emotion.sadness', 'faceAttributes.emotion.surprise',\n",
       "       'faceAttributes.blur.blurLevel', 'faceAttributes.blur.value',\n",
       "       'faceAttributes.exposure.exposureLevel',\n",
       "       'faceAttributes.exposure.value', 'faceAttributes.noise.noiseLevel',\n",
       "       'faceAttributes.noise.value', 'faceAttributes.makeup.eyeMakeup',\n",
       "       'faceAttributes.makeup.lipMakeup', 'faceAttributes.accessories',\n",
       "       'faceAttributes.occlusion.foreheadOccluded',\n",
       "       'faceAttributes.occlusion.eyeOccluded',\n",
       "       'faceAttributes.occlusion.mouthOccluded', 'faceAttributes.hair.bald',\n",
       "       'faceAttributes.hair.invisible', 'faceAttributes.hair.hairColor'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查属性/特征值\n",
    "df_face.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d81a4ecb-ca77-4ba2-b10a-665f18f5c5cb</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.93</td>\n",
       "      <td>25.0</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId faceAttributes.glasses  \\\n",
       "0  d81a4ecb-ca77-4ba2-b10a-665f18f5c5cb              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.age faceAttributes.gender  \n",
       "0                            0.93                25.0                  male  "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可观察其中几组数据\n",
    "df_face[['faceId','faceAttributes.glasses','faceAttributes.emotion.neutral','faceAttributes.age','faceAttributes.gender']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "五、人脸相似度\n",
    "1、（create 列表）请求成功200 返回空字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId =  \"kaka\"#学生填写\n",
    "create_facelists_url =  'https://api-1.cognitiveservices.azure.cn/face/v1.0/facelists/{}' #学生填写\n",
    "subscription_key = '17623c51f2a3403d9048528e6032cd76'#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request header\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "# 学生填写\n",
    "            \"name\": \"player\",\n",
    "        \"userData\": \"照片库\",\n",
    "        \"recognitionModel\": \"recognition_02\",\n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) #学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = \"https://api-1.cognitiveservices.azure.cn/face/v1.0/facelists/{}/persistedFaces\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1035455085,2192695287&fm=26&gp=0.jpg\" #学生填写\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"kaka22\"\n",
    "#学生填写\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '89c85795-5cee-45f1-b55c-0535fcf32d69'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()#返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "六、扩展内容，封装成函数方便多次使用 *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-1.cognitiveservices.azure.cn/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1103189985,1169471293&fm=26&gp=0.jpg\",\"C罗\")\n",
    "AddFace(\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3106205927,2415991774&fm=26&gp=0.jpg\",\"梅西\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "七、get 查看facelists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '89c85795-5cee-45f1-b55c-0535fcf32d69',\n",
       "   'userData': 'kaka22'},\n",
       "  {'persistedFaceId': '46cb2711-d6d8-44ae-9e4f-5367ff1bda68',\n",
       "   'userData': 'C罗'},\n",
       "  {'persistedFaceId': 'd12daa64-4d5a-4b3c-82e0-619393c5bb69',\n",
       "   'userData': '梅西'},\n",
       "  {'persistedFaceId': 'c1bb8db6-ed16-4fb1-b83b-77f95ced6bdd',\n",
       "   'userData': 'C罗'},\n",
       "  {'persistedFaceId': 'c5b22e94-a087-48a7-9190-c7e5b41fc925',\n",
       "   'userData': '梅西'}],\n",
       " 'faceListId': 'kaka',\n",
       " 'name': 'player',\n",
       " 'userData': '照片库'}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url =\"https://api-1.cognitiveservices.azure.cn/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "八、人脸相似度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '8bf3a13d-dee9-4bd6-81f5-29fd5d1ac347',\n",
       "  'faceRectangle': {'top': 159, 'left': 193, 'width': 163, 'height': 163},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 15.7, 'roll': -2.4, 'yaw': 18.2},\n",
       "   'gender': 'male',\n",
       "   'age': 25.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.005,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.061,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.93,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.001},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.14},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.71},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.09},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.02,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.83},\n",
       "     {'color': 'gray', 'confidence': 0.43},\n",
       "     {'color': 'other', 'confidence': 0.35},\n",
       "     {'color': 'blond', 'confidence': 0.07},\n",
       "     {'color': 'red', 'confidence': 0.03}]}}}]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-1.cognitiveservices.azure.cn/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1035455085,2192695287&fm=26&gp=0.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://api-1.cognitiveservices.azure.cn/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"8bf3a13d-dee9-4bd6-81f5-29fd5d1ac347\",#取上方的faceID\n",
    "    \"faceListId\": \"kaka\",\n",
    "    \"maxNumOfCandidatesReturned\": 1,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '89c85795-5cee-45f1-b55c-0535fcf32d69',\n",
       "  'confidence': 1.0}]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89c85795-5cee-45f1-b55c-0535fcf32d69</td>\n",
       "      <td>kaka22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46cb2711-d6d8-44ae-9e4f-5367ff1bda68</td>\n",
       "      <td>C罗</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d12daa64-4d5a-4b3c-82e0-619393c5bb69</td>\n",
       "      <td>梅西</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>c1bb8db6-ed16-4fb1-b83b-77f95ced6bdd</td>\n",
       "      <td>C罗</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>c5b22e94-a087-48a7-9190-c7e5b41fc925</td>\n",
       "      <td>梅西</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData\n",
       "0  89c85795-5cee-45f1-b55c-0535fcf32d69   kaka22\n",
       "1  46cb2711-d6d8-44ae-9e4f-5367ff1bda68       C罗\n",
       "2  d12daa64-4d5a-4b3c-82e0-619393c5bb69       梅西\n",
       "3  c1bb8db6-ed16-4fb1-b83b-77f95ced6bdd       C罗\n",
       "4  c5b22e94-a087-48a7-9190-c7e5b41fc925       梅西"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#facelist里面的数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89c85795-5cee-45f1-b55c-0535fcf32d69</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  89c85795-5cee-45f1-b55c-0535fcf32d69         1.0"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89c85795-5cee-45f1-b55c-0535fcf32d69</td>\n",
       "      <td>kaka22</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "0  89c85795-5cee-45f1-b55c-0535fcf32d69   kaka22         1.0"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并在一起，得出班级能谁最像你\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "九、删除人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "faceListId =\"kaka\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸id\n",
    "delete_face_url =\"https://api-1.cognitiveservices.azure.cn/face/v1.0/facelists/eflower04/persistedfaces/fdda5df3-5b96-4518-814f-046eb3d7cf6a\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"]\n",
    "\n",
    "# 直接取上面获得的ID{'persistedFaceId': 'f18450d3-60d2-45f3-a69e-783574dc3ce8'} \n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url,headers=headers,params=persistedFaceId)#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [404]>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
